package reportsV20210630

import (
	"encoding/json"
	"fmt"
	"gitee.com/HuaLiLiDeDiDiao/amazon-spapi-sdk-go/client"
	"net/url"
	"time"
)

func GetReports(spClient *client.SPAPIClient, request GetReportsRequest) (*GetReportsResponse, error) {
	// 构建 API 路径
	path := "/reports/2021-06-30/reports"

	// 构建 Query 参数
	queryParams := url.Values{}
	if len(request.ReportTypes) > 0 {
		for _, reportType := range request.ReportTypes {
			queryParams.Add("reportTypes", reportType)
		}
	}
	if len(request.ProcessingStatuses) > 0 {
		for _, status := range request.ProcessingStatuses {
			queryParams.Add("processingStatuses", status)
		}
	}
	if len(request.MarketplaceIds) > 0 {
		for _, marketplaceId := range request.MarketplaceIds {
			queryParams.Add("marketplaceIds", marketplaceId)
		}
	}
	if request.PageSize > 0 {
		queryParams.Add("pageSize", fmt.Sprintf("%d", request.PageSize))
	}
	if !request.CreatedSince.IsZero() {
		queryParams.Add("createdSince", request.CreatedSince.Format(time.RFC3339))
	}
	if !request.CreatedUntil.IsZero() {
		queryParams.Add("createdUntil", request.CreatedUntil.Format(time.RFC3339))
	}
	if request.NextToken != "" {
		queryParams.Add("nextToken", request.NextToken)
	}
	// 调用通用请求方法
	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get reports: %w", err)
	}
	// 解析响应数据
	var response GetReportsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}

// CreateReport creates a report based on the request parameters
func CreateReport(spClient *client.SPAPIClient, request CreateReportRequest) (*CreateReportResponse, error) {
	path := "/reports/2021-06-30/reports"
	resp, err := spClient.MakeRequest("POST", path, nil, request)
	if err != nil {
		return nil, fmt.Errorf("failed to create report: %w", err)
	}
	if resp.StatusCode()/100 != 2 {
		return nil, fmt.Errorf(resp.String())
	}
	var response CreateReportResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// GetReport retrieves a specific report based on ReportId
func GetReport(spClient *client.SPAPIClient, request GetReportRequest) (*GetReportResponse, error) {
	path := fmt.Sprintf("/reports/2021-06-30/reports/%s", request.ReportId)

	resp, err := spClient.MakeRequest("GET", path, nil, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get report: %w", err)
	}
	var response GetReportResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// CancelReport cancels a report based on ReportId
func CancelReport(spClient *client.SPAPIClient, request CancelReportRequest) error {
	path := fmt.Sprintf("/reports/2021-06-30/reports/%s", request.ReportId)

	_, err := spClient.MakeRequest("DELETE", path, nil, nil)
	if err != nil {
		return fmt.Errorf("failed to cancel report: %w", err)
	}
	return nil
}

// GetReportSchedules retrieves report schedules for the specified report types
func GetReportSchedules(spClient *client.SPAPIClient, request GetReportSchedulesRequest) (*GetReportSchedulesResponse, error) {
	path := "/reports/2021-06-30/schedules"
	queryParams := url.Values{}
	if len(request.ReportTypes) > 0 {
		for _, reportType := range request.ReportTypes {
			queryParams.Add("reportTypes", reportType)
		}
	}
	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get report schedules: %w", err)
	}

	var response GetReportSchedulesResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}

// CreateReportSchedule creates a report schedule based on the request parameters
func CreateReportSchedule(spClient *client.SPAPIClient, request CreateReportScheduleRequest) (*CreateReportScheduleResponse, error) {
	path := "/reports/2021-06-30/schedules"

	resp, err := spClient.MakeRequest("POST", path, nil, request)
	if err != nil {
		return nil, fmt.Errorf("failed to create report schedule: %w", err)
	}
	var response CreateReportScheduleResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}
func GetReportSchedule(spClient *client.SPAPIClient, request GetReportScheduleRequest) (*GetReportScheduleResponse, error) {
	path := fmt.Sprintf("/reports/2021-06-30/schedules/%s", request.ReportScheduleId)
	resp, err := spClient.MakeRequest("GET", path, nil, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get report schedule: %w", err)
	}
	var response GetReportScheduleResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// CancelReportSchedule cancels a report schedule based on ReportScheduleId
func CancelReportSchedule(spClient *client.SPAPIClient, request CancelReportScheduleRequest) error {
	path := fmt.Sprintf("/reports/2021-06-30/schedules/%s", request.ReportScheduleId)

	_, err := spClient.MakeRequest("DELETE", path, nil, nil)
	if err != nil {
		return fmt.Errorf("failed to cancel report schedule: %w", err)
	}

	return nil
}

// GetReportDocument retrieves the document associated with a report
func GetReportDocument(spClient *client.SPAPIClient, request GetReportDocumentRequest) (*GetReportDocumentResponse, error) {
	path := fmt.Sprintf("/reports/2021-06-30/documents/%s", request.ReportDocumentId)

	resp, err := spClient.MakeRequest("GET", path, nil, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get report document: %w", err)
	}

	var response GetReportDocumentResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}
